#ifndef TIMEOUT_H
#define TIMEOUT_H

#include "Arduino.h"
#include "Scheduler.h"


class Timeout: public Scheduler//,public CallbackBase
{
public:
typedef void (*timeout_callback_ptr)();
Timeout();
~Timeout();
void begin();
void schedule() override;
void update() override;
void cleanup()override;

struct _model_function_object {
    
    struct _class;
    void (_class::*_methodfunc)();
    void *obj;
};

template<typename T,typename U>
void attach(U *obj,void(T::*method)(),uint32_t sec=1)
{
    _cmp_sec = 0;
    _sec = sec;
    _store_obj.obj = obj;
    _store_obj._methodfunc = reinterpret_cast<decltype(_model_function_object::_methodfunc)>(method);
}
template<typename T,typename U>
void detach(U *obj,void(T::*method)())
{
    _store_obj.obj = nullptr;
    _store_obj._methodfunc = nullptr;
}

void attach(timeout_callback_ptr cb,uint32_t sec=1)
{
    _cmp_sec = 0;
    _sec = sec;
    _cb = cb;
}
void detach(timeout_callback_ptr cb=nullptr)
{
    _cb = nullptr;
}
private:
    _model_function_object _store_obj;
    timeout_callback_ptr _cb;
    uint32_t _counter;
    uint32_t _sec;
    uint32_t _cmp_sec;
};

#endif